﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Globalization;
using System.Collections;

namespace Dzzzr
{
	/// <summary>
	/// TypeConverter for EntityAttribute list
	/// </summary>
	class EntityAttributeTypeConverter : TypeConverter
	{
		/// <summary>
		/// EntityTypes
		/// </summary>
		Dzzzr.EntityAttributeType[] entityAttributeTypes = null;
		/// <summary>
		/// EntityTypes
		/// </summary>
		Dzzzr.EntityAttributeType[] EntityAttributeTypes
		{
			get
			{
				if (entityAttributeTypes == null)
					entityAttributeTypes = (from ea in DataContext.EntityAttributeTypes select ea).ToArray();
				return entityAttributeTypes;
			}
		}

		/// <summary>
		/// Database context
		/// </summary>
		protected MonitoringDBDataContext DataContext
		{
			get
			{
				return DBDataContext.DataContext;
			}
		}

		/// <summary>
		/// Check convert
		/// </summary>
		/// <param name="context"></param>
		/// <param name="destType"></param>
		/// <returns></returns>
		public override bool CanConvertTo(ITypeDescriptorContext context, Type destType)
		{
			return destType == typeof(string);
		}

		/// <summary>
		/// Convert
		/// </summary>
		/// <param name="context"></param>
		/// <param name="culture"></param>
		/// <param name="value"></param>
		/// <param name="destType"></param>
		/// <returns></returns>
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destType)
		{
			if (value is EntityAttributeType)
			{
				return (value as EntityAttributeType).Name;
			}
			return string.Empty;
		}

		/// <summary>
		/// Check convert
		/// </summary>
		/// <param name="context"></param>
		/// <param name="srcType"></param>
		/// <returns></returns>
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type srcType)
		{
			return srcType == typeof(string);
		}

		/// <summary>
		/// Convert
		/// </summary>
		/// <param name="context"></param>
		/// <param name="culture"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			var val = (from ea in EntityAttributeTypes where ea.Name == Convert.ToString(value) && ea.EntityTypeID == (context.Instance as EntityAttribute).Entity.EntityTypeID select ea);
			if (val.Count() > 0)
				return val.First();
			return null;
		}

		/// <summary>
		/// Allow custom values
		/// </summary>
		public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
		{
			return true;
		}

		/// <summary>
		/// Only list elements
		/// </summary>
		public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
		{
			return true;
		}

		/// <summary>
		/// Values list
		/// </summary>
		public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
		{
			ICollection items = (from ea in EntityAttributeTypes where ea.EntityTypeID == (context.Instance as EntityAttribute).Entity.EntityTypeID orderby ea.ID select ea).ToArray();
			return new StandardValuesCollection(items);
		}
	}
}
